package com.chen.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.entity.User;
import com.chen.entity.UserActive;
import com.chen.entity.vo.ActiveVo;
import com.chen.mapper.ActiveMapper;
import com.chen.mapper.UserActiveMapper;
import com.chen.mapper.UserMapper;
import com.chen.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.common.Result;
import com.chen.service.IActiveService;
import com.chen.entity.Active;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Y·C
 * @since 2023-04-24
 */
@Api(tags = "活动接口")
@RestController
@RequestMapping("/active")
public class ActiveController {

    @Resource
    private IActiveService activeService;
    @Resource
    private ActiveMapper activeMapper;
    @Resource
    private UserActiveMapper userActiveMapper;
    @Resource
    private UserMapper userMapper;


    @ApiOperation("添加或者修改活动")
    @PostMapping
    public Result save(@RequestBody ActiveVo activeVo) {
        Active active = new Active();
        //将activeVo信息复制给mate
        BeanUtil.copyProperties(activeVo,active, true);
        activeService.saveOrUpdate(active);
        return Result.success();
    }

//    @DeleteMapping("/{id}")
//    public Result delete(@PathVariable Integer id) {
//        activeService.removeById(id);
//        return Result.success();
//    }

    @ApiOperation("更具id删除多条或者一条")
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        activeService.removeByIds(ids);
        return Result.success();
    }

    @ApiOperation("查询全部活动")
    @GetMapping
    public Result findAll() {
        return Result.success(activeService.list());
    }

//    @GetMapping("/{id}")
//    public Result findOne(@PathVariable Integer id) {
//        return Result.success(activeService.getById(id));
//    }

    @ApiOperation("条件(活动名称)查询分页显示全部活动")
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String activeName) {
        QueryWrapper<Active> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.like("active_name",activeName);
        return Result.success(activeService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }


    @ApiOperation("报名接口")
    @PostMapping("/enrollment/{id}")
    public Result enrollment(@PathVariable Integer id){

        User user = TokenUtils.getCurrentUser();
        Integer userId = user.getId();
        Active active = activeMapper.selectById(id);
        UserActive userActive = new UserActive();
        userActive.setActiveId(id);
        userActive.setUserId(userId);

        userActiveMapper.insert(userActive);
        Integer manNumber = active.getManNumber();

        Integer womanNumber = active.getWomanNumber();

        if ("男".equals(user.getSex())){
            manNumber = manNumber+1;
            active.setManNumber(manNumber);
        }else {
            womanNumber = womanNumber+1;
            active.setWomanNumber(womanNumber);
        }

        return Result.success(activeMapper.updateById(active));
    }

    @ApiOperation("报名会员信息 传活动id")
    @PostMapping("/userInfo/{id}")
    public Result userInfo(@PathVariable Integer id){

        List<User> infoList = userMapper.selectListInfo(id);

        return Result.success(infoList);
    }



}

